package com.northpool.operator.statistics.dataset.math;

import com.alibaba.fastjson.JSONObject;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.common.algorithm.annotation.PluginInfo;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.type.TypeEnum;
import com.northpool.resources.command.QueryFilter;
import com.northpool.resources.datatable.FieldEncoder;
import com.northpool.resources.datatable.ITable;
import com.northpool.resources.datatable.dao.IMapDAO;
import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OutputParam;
import com.northpool.operator.annotation.OutputParams;
import org.apache.commons.math3.stat.inference.TTest;

import java.util.*;

@OperatorInfo(
        ref = "StatisticalSingleSampleMeanTTest",
        name = "差异性分析：数据TTest单样本均值检验"
)
@OutputParams({
        @OutputParam(ref = "statisticalResults", name = "统计结果", type = TypeEnum.STRING)
})
public class StatisticalSingleSampleMeanTTest  extends BaseMathStatistical {

    @InputParams({
            @InputParam(ref = "dataSourceId", name = "数据源ID", desc = "数据源ID", type = TypeEnum.DATASOURCE, required = true),
            @InputParam(ref = "tableName", name = "数据表名称", desc = "数据表名称，如: public.table", type = TypeEnum.STRING),
            @InputParam(ref = "infoFieldName", name = "字段名称", desc = "字段名称，如: chinese_score,math_score,physics_score", type = TypeEnum.STRING),
            @InputParam(ref = "statisticValue", name = "值字段名称", desc = "值字段名称，如: 88", type = TypeEnum.STRING)
    })
    public StatisticalSingleSampleMeanTTest() {
    }

    @Override
    protected void executeStatistical(ParamTool paramTool)  {
        String infoFieldName= paramTool.getStr("infoFieldName");
        Double statisticValue= Double.parseDouble(paramTool.getStr("statisticValue"));

        try {
            String[] fields = infoFieldName.split(",");
            ITable table = tableOperator.getTable(fields, FieldEncoder.ORIGIN_FIELD_ENCODER);
            IMapDAO<Object> dao = table.mapDao();
//            this.progress(99);

            Boolean statisticalResults = exectuteSampleMean(dao,fields[0],statisticValue);

            String metaStr = JSONObject.toJSONString(statisticalResults, true);
            this.writeResult(metaStr);
            this.successExit();
        }catch (Exception ex) {
            this.error(ex.getMessage());
            this.failedExit();
        }
    }

    private Boolean exectuteSampleMean(IMapDAO<Object> dao,String fieldName,  Double statisticValue) {
        QueryFilter filter = new QueryFilter();
        List<Map<String, Object>> queryResultData = dao.query(filter);
        List<Double> listDataValue=new ArrayList<>();
        for (int i = 0; i < queryResultData.size(); i++) {
                listDataValue.add(Double.parseDouble(queryResultData.get(i).get(fieldName).toString()));
        }

        double populationMean = statisticValue;  // 假设总体均值
        TTest tTest = new TTest();
        double pValueA = tTest.tTest(populationMean, listDataValue.stream().mapToDouble(d -> d).toArray()
        );
        return pValueA < 0.05;
    }
}
